package project_package;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class EmployeeRecords
{
	private static EmployeeRecords instance = null;
    HashMap<Integer, Employee> employees = new HashMap<Integer, Employee>();
    int employeeIndex = 0;
    
    // we're going from array list to hash map as it's faster and better
    //ArrayList<Employee> employees = new ArrayList<Employee>(); 
    
    public static EmployeeRecords getInstance()
    {
    	if(instance == null)
    	{
    		instance = new EmployeeRecords();
    	}
    	return instance;
    }
    
    // changed from void to boolean
    // if created employee returns true
    public boolean createEmployee(String name, String phoneNo, String address, long civilNum, double salary, boolean isManager, String password, double salesTotal, int salesNumber)
    {
    	boolean notFound = true;
    	
    	Set<Map.Entry<Integer, Employee>> st = employees.entrySet();
 
    	for(Map.Entry<Integer, Employee> employee : st)
    	{  
    	      if (employee.getValue().getName().equals(name))
    	    	  notFound = false;
    	} 
    	
    	if (notFound) 
    	{
	        Employee newEmployee = new Employee(employeeIndex, name, phoneNo, address, civilNum, salary, isManager, password, salesTotal, salesNumber);
	        this.employees.put(employeeIndex, newEmployee);
	        employeeIndex++;
    	}
    	return notFound;
    }
        
    public boolean updateEmployee(int id, String name, String phoneNo, String address, long civilNum, long datestamp, double salary, boolean isManager, String password, double salesTotal, int salesNumber) // int -> long
	{
		boolean found = false;
		
		Set<Map.Entry<Integer, Employee>> st = employees.entrySet();
		 
    	for(Map.Entry<Integer, Employee> employee : st)
    	{  
    	      if (employee.getKey().equals(id))
    	      {
    	    	  found = true;
    	    	  employee.getValue().setName(name);
    	    	  employee.getValue().setPhoneNo(phoneNo);
    	    	  employee.getValue().setAddress(address);
    	    	  employee.getValue().setCivilNum(civilNum);
    	    	  employee.getValue().setDatestamp(datestamp);
    	    	  employee.getValue().setSalary(salary);
    	    	  employee.getValue().setIsManager(isManager);
    	    	  employee.getValue().setPassword(password);
    	    	  employee.getValue().setSalesTotal(salesTotal);
    	    	  employee.getValue().setSalesNumber(salesNumber);
    	      }
    	} 
    	
		return found;
	}
       
    public boolean deleteEmployee(String name)
    {
    	Employee employee = findEmployee(name);
    	if (employee != null) 
    	{
    		int id = employee.getID();
	        employees.remove(id);
	        return true;
    	} 
    	else {
    		return false;
    	}
    }
    
    public Employee findEmployee(String name)
	{
    	Set<Map.Entry<Integer, Employee>> st = employees.entrySet();
		 
    	for(Map.Entry<Integer, Employee> employee : st)
    	{  
    	      if (employee.getValue().getName().equals(name))
    	      {
    	    	  return employee.getValue();
    	      }
    	}
    	return null;
    }
    
    public void printEmployees()
	{
    	if (employees.isEmpty()) {
			System.out.println("No employees were found in the database");
		}
		else {
	    	Employee tempEmployee;
	    	
			Set<Map.Entry<Integer, Employee>> st = employees.entrySet();

			for(Map.Entry<Integer, Employee> employee : st)
	    	{  
	    	    	  tempEmployee = employee.getValue();
	    	    	  System.out.println("Employee ID: " + tempEmployee.getID() +
	    	    			  " - Employee Name: " + tempEmployee.getName() + 
	    	    			  " - Employee Type: " + (tempEmployee.getIsManager() ? "Manager" : "Normal") +
	    	    			  " - Employee Salary: " + tempEmployee.getSalary() +
	    	    			  " - Employee Sales Total Price: " + tempEmployee.getSalesTotal() +
	    	    			  " - Employee Sales Total Number: " + tempEmployee.getSalesNumber() +
	    	    			  " - Employee Since: " + tempEmployee.getDate());
			}
		}
	}
    
    public HashMap<Integer, Employee> getEmployees() {
		return employees;
	}	
    
    public int getEmployeeNumber()
    {
        int employeeNumber = employees.size();
        return employeeNumber;
    }
}